unit Session;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, StdCtrls, ComCtrls, Buttons, ExtCtrls, DB, Grids,
  HttpProt, GIFImg, ImgList,
  DBTables, Gauges, FMTBcd, SqlExpr, DBGrids, PortalGrid;

type
  TFormSession = class(TForm)
    Bevel6: TBevel;
    Label5: TLabel;
    BtnClose: TBitBtn;
    BtnClear: TBitBtn;
    BtnClearAll: TBitBtn;
    CheckLogging: TCheckBox;
    DSSessions: TDataSource;
    GridSessions: TPortalGrid;
    procedure FormCreate(Sender: TObject);
    procedure GridSessionsKeyPress(Sender: TObject; var Key: Char);
    procedure GridSessionsTitleButtonClick(Sender: TObject;
      AFieldName: String);
    procedure BtnCloseClick(Sender: TObject);
    procedure GridSessionsCalcTitleAttributes(Sender: TObject;
      AFieldName: String; AFont: TFont; ABrush: TBrush;
      var ATitleAlignment: TAlignment);
    procedure BtnClearClick(Sender: TObject);
    procedure BtnClearAllClick(Sender: TObject);
  private
    { Private declarations }
    procedure UpdateQuery(f: string);
  public
    { Public declarations }
  end;

var
  FormSession: TFormSession;

implementation
uses
   DBClient;

{$R *.DFM}

var
  sorting: string;

procedure TFormSession.FormCreate(Sender: TObject);
var d, h, m: integer;
begin
  with Datawell.TableSessions do begin
    active := true;
    first;
    while not EOF do begin
      m:=fieldbyname('minutes').asinteger;
      h:=m div 60;
      dec(m,h*60);
      d:=h div 24;
      dec(h,d*24);
      edit;
      fieldbyname('DDays').asinteger:=d;
      fieldbyname('DHours').asinteger:=h;
      fieldbyname('DMinutes').asinteger:=m;
      post;
      next;
    end;
    first;
  end;
  UpdateQuery('DDays');
  CheckLogging.checked:=Datawell.TableGlobals.fieldbyname('L13').asboolean;
end;

procedure TFormSession.BtnCloseClick(Sender: TObject);
begin
  with Datawell.TableGlobals do begin
    edit;
    fieldbyname('L13').asboolean:=CheckLogging.checked;
    post;
  end;
  close;
end;

procedure TFormSession.GridSessionsKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnClose.click;
  end;
end;

procedure TFormSession.GridSessionsTitleButtonClick(Sender: TObject;
  AFieldName: String);
begin
  UpdateQuery(afieldname);
end;

procedure TFormSession.GridSessionsCalcTitleAttributes(Sender: TObject;
  AFieldName: String; AFont: TFont; ABrush: TBrush;
  var ATitleAlignment: TAlignment);
begin
  if (afieldname = 'Host') or (afieldname = 'DDays') or
     (afieldname = 'Created') or (afieldname = 'Connections') then begin
    afont.style:=[fsbold];
    afont.size:=10;
  end
  else begin
    afont.style:=[];
    afont.size:=8;
  end;
  if afieldname = sorting then afont.color:=clblue
  else afont.color:=clblack;
end;

procedure TFormSession.BtnClearAllClick(Sender: TObject);
var table: TClientDataset;
begin
  if messagedlg('Are you sure?',mtconfirmation,[mbyes,mbno],0) = mrno then exit;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableSessions, true);
    active:=true;
    first;
    while not EOF do delete;
  end;
  table.free;
  GridSessions.setfocus;
end;

procedure TFormSession.BtnClearClick(Sender: TObject);
var
  table: TClientDataset;
  hp: string;
begin
  hp:=Datawell.TableSessions.fieldbyname('Host').asstring+':'+
    Datawell.TableSessions.fieldbyname('Port').asstring;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableSessions, false, true);
    active:=true;
    first;
    while not EOF do begin
      if hp = fieldbyname('Host').asstring+':'+fieldbyname('Port').asstring then begin
        delete;
        break;
      end;
      next;
    end;
  end;
  table.free;
  GridSessions.setfocus;
end;

procedure TFormSession.UpdateQuery(f: string);
const
  QUERY = 'SELECT * FROM Sessions ORDER BY %s';
begin
  if (f <> 'Host') and (f <> 'DDays') and (f <> 'Created') and (f <> 'Connections') then exit;
  GridSessions.hide;
  with Datawell.TableSessions do
  begin
    active:=false;
    if f = 'Host' then
      dataset.commandText := format(QUERY, ['Host ASC, Port ASC'])
    else if f = 'DDays' then
      dataset.commandText := format(QUERY, ['DDays DESC, DHours DESC, DMinutes DESC'])
    else if f = 'Created' then
      dataset.commandText := format(QUERY, ['Created ASC'])
    else if f = 'Connections' then
      dataset.commandText := format(QUERY, ['Connections DESC']);
    active:=true;
    first;
  end;
  GridSessions.show;
  sorting:=f;
end;

end.
